package example.json.msg.impl;

import example.json.msg.MsgBaseService;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import static example.json.msg.util.PhoneNumberUtil.*;

@SuppressWarnings("all")
@Repository
@Data
@Setter
@Getter
public class GuoduExample extends BaseHelper implements MsgBaseService {

    @Override
    public String batchSend(String accType, String phones, String content) {
        LOG.info("content:" + content);
        String guodu_acc, guodu_pwd;
        //转换行业类营销类短信
        switch(accType){
            case YZM:
                guodu_acc = guodu_yzm_acc;
                guodu_pwd = guodu_yzm_pwd;
                break;
            case YX:
                guodu_acc = guodu_yx_acc;
                guodu_pwd = guodu_yx_pwd;
                break;
            default:
                guodu_acc = guodu_hy_acc;
                guodu_pwd = guodu_hy_pwd;
                break;
        }
        JSONObject gdata = new JSONObject();
        gdata.put("OperID", guodu_acc);
        gdata.put("OperPass", guodu_pwd);
        gdata.put("AppendId", getAppendId(content));
        gdata.put("SendTime", sendtime("yyyyMMddHHmmss"));
        gdata.put("ValidTime", tommorrow());
        if (StringUtils.isNotEmpty(phones)) {
            gdata.put("DesMobile", phones);
        } else {
            gdata.put("DesMobile", phone(1));
        }
        gdata.put("Content", content);
        return httpClient(guodu_master_url)
            .doPostParams(gdata.toString(),"GBK");
    }

    @Override
    public String mutipleSend(String accType, String content) {
        String guodu_acc, guodu_pwd;
        //转换行业类营销类短信
        switch(accType){
            case YZM:
                guodu_acc = guodu_yzm_acc;
                guodu_pwd = guodu_yzm_pwd;
                break;
            case YX:
                guodu_acc = guodu_yx_acc;
                guodu_pwd = guodu_yx_pwd;
                break;
            default:
                guodu_acc = guodu_hy_acc;
                guodu_pwd = guodu_hy_pwd;
                break;
        }
        JSONObject gdata = new JSONObject();
        gdata.put("OperID", guodu_acc);
        gdata.put("OperPass", guodu_pwd);
        gdata.put("AppendId", getAppendId(content));
        gdata.put("SendTime", sendtime("yyyyMMddHHmmss"));
        gdata.put("ValidTime", tommorrow());

        //切分为两部分：num1,num2,num3|内容1,内容2,内容3
        String[] contentStr = content.split("|");
        if (StringUtils.isNotEmpty(content)) {
            gdata.put("DesMobile", contentStr[0]);
            gdata.put("Content", contentStr[1]);
        } else {
            gdata.put("DesMobile", phone(3));
            gdata.put("Content", "content1,content2,content3");
        }

        return httpClient(guodu_multiple_url)
                .doPostParams(gdata,"GBK");
    }

    @Override
    public String voice(String accType, String phones, String content) {
        LOG.info("content:" + content);
        if (!content.contains("验证码")) {
            return "error 必须包含关键字：验证码";
        }
        String guodu_acc = guodu_voice_acc, guodu_pwd = guodu_voice_pwd;
        JSONObject gdata = new JSONObject();
        gdata.put("OperID", guodu_acc);
        gdata.put("OperPass", guodu_pwd);
        gdata.put("AppendId", "0000");
        gdata.put("SendTime", sendtime("yyyyMMddHHmmss"));
        gdata.put("ValidTime", tommorrow());
        if (StringUtils.isNotEmpty(phones)) {
            gdata.put("DesMobile", phones);
        } else {
            gdata.put("DesMobile", phone(1));
        }
        gdata.put("Content", content);
        return httpClient(guodu_master_url)
                .doPostParams(gdata.toString(),"GBK");
    }

    @Override
    public String report(String accType) {
        JSONObject gdata = new JSONObject();
        String guodu_acc = guodu_yzm_acc, guodu_pwd = guodu_yzm_pwd;
        //转换行业类营销类短信
        switch(accType){
            case YZM:
                guodu_acc = guodu_yzm_acc;
                guodu_pwd = guodu_yzm_pwd;
                break;
            case YX:
                guodu_acc = guodu_yx_acc;
                guodu_pwd = guodu_yx_pwd;
                break;
            default:
                guodu_acc = guodu_hy_acc;
                guodu_pwd = guodu_hy_pwd;
                break;
        }
        gdata.put("OperID", guodu_acc);
        gdata.put("OperPass", guodu_pwd);

        return httpClient(guodu_mt_url)
                .doPostParams(gdata,"GBK");
    }

    @Override
    public String sms(String accType) {
        JSONObject gdata = new JSONObject();
        String guodu_acc = guodu_yzm_acc, guodu_pwd = guodu_yzm_pwd;
        //转换行业类营销类短信
        switch(accType){
            case YZM:
                guodu_acc = guodu_yzm_acc;
                guodu_pwd = guodu_yzm_pwd;
                break;
            case YX:
                guodu_acc = guodu_yx_acc;
                guodu_pwd = guodu_yx_pwd;
                break;
            default:
                guodu_acc = guodu_hy_acc;
                guodu_pwd = guodu_hy_pwd;
                break;
        }
        gdata.put("OperID", guodu_acc);
        gdata.put("OperPass", guodu_pwd);

        return httpClient(guodu_mo_url)
                .doPostParams(gdata,"GBK");
    }

    @Override
    public String balance(String accType) {
        JSONObject gdata = new JSONObject();
        String guodu_acc = guodu_yzm_acc, guodu_pwd = guodu_yzm_pwd;
        //转换行业类营销类短信
        switch(accType){
            case YZM:
                guodu_acc = guodu_yzm_acc;
                guodu_pwd = guodu_yzm_pwd;
                break;
            case YX:
                guodu_acc = guodu_yx_acc;
                guodu_pwd = guodu_yx_pwd;
                break;
            default:
                guodu_acc = guodu_hy_acc;
                guodu_pwd = guodu_hy_pwd;
                break;
        }
        gdata.put("OperID", guodu_acc);
        gdata.put("OperPass", guodu_pwd);
        gdata.put("AppendId", "0000");

        return httpClient(guodu_balance_url)
                .doPostParams(gdata,"GBK");
    }

    private String getAppendId(String content) {
        if (content.contains("【涅槃】")) {
            return "0000";
        } else if (content.contains("【钱站】")) {
            return "0001";
        } else if (content.contains("【两个狒狒】")) {
            return "0002";
        } else if (content.contains("【钱站营销】")) {
            return "0003";
        } else if (content.contains("【凡普信】")) {
            return "0005";
        }
        return "9999";
    }
}
